package com.online.olsrootserver.handler;

import com.online.olsrootserver.service.CustomerService;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Created by littleheng on 2018/4/26
 */
@Component
public class UnsubscribeHandler extends AbstractHandler {

    @Autowired
    private CustomerService customerService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMpXmlMessage, Map<String, Object> map, WxMpService wxMpService, WxSessionManager wxSessionManager) throws WxErrorException {

        String openId = wxMpXmlMessage.getFromUser();
        this.logger.info("取消关注用户 OPENID: " + openId);

        //  更新本地数据库顾客状态为取消关注状态
        int result = this.customerService.modifySubscribeStatus(openId, "0");
        if (result > 0) {
            this.logger.info("更新顾客关注状态成功");
        }
        return null;
    }
}